package com.zxzb.servlet;
import com.google.gson.Gson;
import com.zxzb.dao.YonghuDao;
import com.zxzb.entity.Yonghu;
import com.zxzb.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "YonghuServlet",urlPatterns = "/YonghuServlet")
public class YonghuServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        request.setCharacterEncoding("utf-8");
        YonghuDao yonghuDao=new YonghuDao();
        //这里类型要设置未text/json
        response.setContentType("text/html;charset=utf-8");
        if(method.equals("login")){//登录
            String account=request.getParameter("account");
            String pwd=request.getParameter("pwd");
            String type=request.getParameter("type");
            List<Object> objects=new ArrayList<>();
            objects.add(account);
            objects.add(pwd);
            objects.add(type);
            //去数据库查询数据
            Yonghu yonghu=yonghuDao.login(objects);
            //将当前用户信息转化为json传回给前端（用的JSON的转化方法）
            PrintWriter writer = response.getWriter();
            if(yonghu!=null){//登录成功
                request.getSession().setAttribute("yonghu",yonghu);
                writer.write(new Gson().toJson(Result.reOk("登录成功")));
            }else{//账号密码错误
                writer.write(new Gson().toJson(Result.reError("账号密码错误")));
            }
        }else if(method.equals("register")){//注册
            String name=request.getParameter("name");
            String account=request.getParameter("account");
            String pwd=request.getParameter("pwd");
            String type=request.getParameter("type");
            //将当前用户信息转化为json传回给前端（用的JSON的转化方法）
            PrintWriter writer = response.getWriter();
            List<Object> exit=new ArrayList<>();
            exit.add(account);
            exit.add(type);
            //验证数据库账号是否存在
            Yonghu yonghu=yonghuDao.exitAccount(exit);
            if(yonghu!=null){
                writer.write(new Gson().toJson(Result.reError("账号已存在，请重新更换账号")));
            }else{
                List<Object> objects=new ArrayList<>();
                objects.add(name);
                objects.add(account);
                objects.add(pwd);
                objects.add(type);
                int count=yonghuDao.register(objects);
                if(count==1){//注册成功
                    writer.write(new Gson().toJson(Result.reOk("注册成功")));
                }else{//注册失败
                    writer.write(new Gson().toJson(Result.reError("注册失败")));
                }
            }
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }


}
